package com.kylin.module.community.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.kylin.common.paginate.Paging;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.kylin.common.Constants;
import com.kylin.module.community.business.SitemsgBlogic;
import com.kylin.module.community.orm.Sitemsg;
import com.kylin.module.user.orm.User;

import com.kylin.module.community.forms.SitemsgForm;
import com.kylin.struts.BaseActionInterface;
import com.kylin.struts.BaseDispatchAction;

public class SitemsgAction  extends BaseDispatchAction implements BaseActionInterface {

	protected static Logger log = Logger.getLogger(MemberAction.class);	
	
 
	public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
		log.info("in action sitemsg.save");
		String forward = "show";
		
		SitemsgForm sitemsgform = (SitemsgForm) form;
		
		log.info(sitemsgform.toString());
		
		SitemsgBlogic logic = new SitemsgBlogic();
		User user = (User)request.getSession().getAttribute(Constants.CURR_USER);
		Long user2id = null;
		if (sitemsgform.getUser2id() != null ){
			log.info(" user2id:" + sitemsgform.getUser2id());
			user2id = sitemsgform.getUser2id();
		}
		
		//if (user == null){
			//log.info("no user session");
			//forward = "login";
		//}else{					
		
			try{				
				logic.save(user, sitemsgform);				
				log.info("保存 message：");
			}catch (Exception e) {
				forward = doException( request, e, forward);
			}
		//}
		log.info("forward to " + forward);
		//return mapping.findForward(forward);
		
		String path = null;//	mapping.findForward("succ").getPath();
		path = "/module/community/member/post.do?task=mypost&uid="+user2id;
		log.info("path: "+path);

		ActionForward actionforward = new ActionForward(path);
		actionforward.setRedirect(true);
		return actionforward;		
		
	}

	public ActionForward search(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		return null;
	}	

	public ActionForward add(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		String forward = "add";
		//User user = null;
		String uid = request.getParameter("uid");
		/*
		if(request.getSession().getAttribute(Constants.CURR_USER)== null){
		   request.setAttribute("forward", "msg.add");
		   request.setAttribute("uid", uid);
		   forward = "login";
		}
		else{
		*/
		//User user = (User)request.getSession().getAttribute(Constants.CURR_USER);
		if (request.getParameter("uid") != null){
				
			request.setAttribute("user2id", uid);
		}else{
			request.setAttribute("user2id", "1");
		}
		//}
		return mapping.findForward(forward);
	
	}		
	
	public ActionForward reply(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		String forward = "reply";
		User user = null;
		Long pid = new Long(request.getParameter("pid"));
		SitemsgBlogic bean = new SitemsgBlogic();
		Sitemsg msg = bean.findById(pid);
		request.setAttribute("msg", msg);
		
		
		String uid = request.getParameter("uid");	
		if(request.getSession().getAttribute(Constants.CURR_USER)== null){
		   request.setAttribute("forward", "msg.add");
		   request.setAttribute("uid", uid);
		   forward = "login";
		}
		else{
			user = (User)request.getSession().getAttribute(Constants.CURR_USER);
			if (request.getParameter("uid") != null){
					
				request.setAttribute("user2id", uid);
			}else{
				request.setAttribute("user2id", "1");
			}
		}
		return mapping.findForward(forward);
	
	}	
	
	public ActionForward delete(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response){
		log.info("in msg.delete");
		Long pid = new Long(request.getParameter("mid"));
		SitemsgBlogic bean = new SitemsgBlogic();
		bean.deleteById(pid);
		//String forward ="list";
		//return mapping.findForward(forward);
		return this.show(mapping, actionForm, request, response);
	}
	
	
	public ActionForward show(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		
		String forward = "show";
		log.info("in sitemsg.show : " + forward);
		Integer limitstart = new Integer("0");
		Integer limit = new Integer("10");
		if(request.getParameter("limitstart") != null )	{		
			limitstart = new Integer (request.getParameter("limitstart"));
		}	
		if(request.getParameter("limit") != null ){
			limit = new Integer (request.getParameter("limit"));
		}
					
		User user = (User)request.getSession().getAttribute(Constants.CURR_USER);
		//Long userid = new Long("1");
		//User user = BaseHibernateDAO.
		if (user == null){
			forward = "login";
		}else{
			
			
			try {

				SitemsgBlogic bean = new SitemsgBlogic();			
				Paging page = bean.show(user, limitstart, limit);			
				request.setAttribute("sitemsgs", page);			
			
			}catch (Exception e) {
				doException( request, e, forward);
			}
			
		}
			
		
		return mapping.findForward(forward);
	}
		
	public ActionForward edit(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response){

		return null;
	}

	public ActionForward update(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response){

		return null;
	}

}
